package linwen.wang;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import static java.util.concurrent.TimeUnit.SECONDS;

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor executor(){
        int processors = Runtime.getRuntime().availableProcessors();
        int corePoolSize = processors*3;
        int maxPoolSize = processors*5;
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,maxPoolSize,10,SECONDS,
                new ArrayBlockingQueue<>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        return executor;
    }
}
